package com.wtwd.device.handler.processimpl;


import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.Constant;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.handler.MsgHandlerFactory;
import com.wtwd.device.handler.ProcessMessageHandler;
import com.wtwd.device.model.DeviceSession;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.netty.LocalSessions;
import com.wtwd.device.properties.MqProperties;
import com.wtwd.device.protocol.AppProtocol;
import io.netty.channel.Channel;

/**
 * 登录指令特殊处理
 * 1.建立设备初始化连接
 * 2.推送远程服务认证设备
 */
@MsgType(MsgTypeEnum.APP_LOGIN)
public class LoginHandler implements ProcessMessageHandler {


    @Override
    public AppProtocol processMsg(AppProtocol appProtocol, Channel channel) {
        DeviceSession session = DeviceSession.builder()
                .imei(appProtocol.getImei())
                .channel(channel)
                .build();
        LocalSessions.channelEstablishment(appProtocol.getImei(), session);
        MsgHandlerFactory.getMqSendMessage()
                .push(
                        MessageBase.builder()
                                .deviceKey(appProtocol.getImei())
                                .appProtocol(appProtocol)
                                .form(MqProperties.getAppTopic())
                                .type(Constant.APP_MSG)
                                .build());
        return null;
    }
}
